import json
import threading
import time
from pytdx.hq import TdxHq_API
from pytdx.params import TDXParams
from conf import config
import pandas as pd
from handler import security

api = TdxHq_API(raise_exception=False)
config.load_conf()

sz = []
sh = []


def unix():
    t = time.time()
    return int(round(t * 1000))


def for_each_host():
    for h in config.hosts:
        with api.connect(str(h.ip), 7709):
            start = unix()
            api.do_heartbeat()
            end = unix()
            print(h.host_name + ': ', end - start, ' ms')
            start = unix()
            api.do_heartbeat()
            end = unix()
            print(h.host_name + ': ', end - start, ' ms')


# for_each_host()
# with api.connect('110.41.147.114', 7709):
#     # data = api.get_security_bars(9, 0, '000001', 0, 10)
#     # data = api.get_history_minute_time_data(TDXParams.MARKET_SZ, '002890', 20221202)
#     data = api.get_security_quotes([(0, '000001'), (1, '600300')])
#     json_str = json.dumps(data)
#     print(json_str)
def get_stock_list():
    with api.connect('110.41.147.114', 7709):
        start = 0
        while True:
            data = api.get_security_list(1, start)
            if data is None:
                break
            start += len(data)
            for d in data:
                sz.append(d['code'])
        # start = 0
        # while True:
        #     data = api.get_security_list(1, start)
        #     if data is None:
        #         break
        #     start += len(data)
        #     for d in data:
        #         sh.append(d['code'])


#
# get_stock_list()
# print(len(sz))
# print(len(sh))

def get_security_quotes():
    with api.connect('110.41.147.114', 7709):
        data = api.get_security_quotes([(0, '002866')])
        for stock in data:
            print('=============================')
            print(stock['ask5'], stock['ask_vol5'])
            print(stock['ask4'], stock['ask_vol4'])
            print(stock['ask3'], stock['ask_vol3'])
            print(stock['ask2'], stock['ask_vol2'])
            print(stock['ask1'], stock['ask_vol1'])
            print('------------------------------')
            print(stock['bid1'], stock['bid_vol1'])
            print(stock['bid2'], stock['bid_vol2'])
            print(stock['bid3'], stock['bid_vol3'])
            print(stock['bid4'], stock['bid_vol4'])
            print(stock['bid5'], stock['bid_vol5'])
            print('=============================')


def list_security():
    # security.locker.acquire()
    for i in range(1000000):
        # security.securityMap['aa'] += 1
        security.count += 1
    # security.locker.release()
    # print(security.securityMap)
    print('count == {}'.format(security.count))


if __name__ == '__main__':
    security.security_map = {
        "aa": 0,
        "bb": 0
    }
    threads = []
    for i in range(3):
        t = threading.Thread(target=list_security)
        threads.append(t)

    for th in threads:
        th.start()

    for th in threads:
        th.join()